package com.ssyt.hiring.company.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.common.PageModel;
import com.ssyt.hiring.common.util.AppUtil;
import com.ssyt.hiring.company.entity.ApplicationEntity;
import com.ssyt.hiring.company.entity.PositionEntity;
import com.ssyt.hiring.company.service.ApplicationService;
import com.ssyt.hiring.company.service.PositionService;
import com.ssyt.hiring.talent.entity.ResumeEntity;
import com.ssyt.hiring.talent.service.ResumeService;

/**
 * PositionServlet
 * 
 * @author Administrator
 *
 */
public class PositionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PositionService positionService = new PositionService();
	private ResumeService resumeService = new ResumeService();
	private ApplicationService applicationService = new ApplicationService();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("queryAll".equals(command)) {
			queryAll(request, response);
		} else if ("detail".equals(command)) {
			detail(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("preupdate".equals(command)) {
			preupdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("toAddUI".equals(command)) {
			toAddUI(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("queryByKeyWord".equals(command)) {
			queryByKeyWord(request, response);
		} else if ("loadingData".equals(command)) {
			loadingData(request, response);
		}
	}

	/**
	 * 按关键字查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void queryByKeyWord(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String keyWord = request.getParameter("keyWord");
		int pageNo = 1;
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null && !"".equals(pageNoStr)) {
			pageNo = Integer.parseInt(pageNoStr);
		}

		PageModel<PositionEntity> pageModel = positionService.queryByKeyWord(
				pageNo, Constants.PAGE_SIZE, keyWord);
		request.setAttribute("pageModel", pageModel);
		request.setAttribute("keyWord", keyWord);
		request.getRequestDispatcher("/WEB-INF/view/company/position-list.jsp")
				.forward(request, response);
	}

	/**
	 * 按关键字查询懒加载
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void loadingData(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String keyWord = request.getParameter("keyWord");
		int pageNo = 1;
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null && !"".equals(pageNoStr)) {
			pageNo = Integer.parseInt(pageNoStr);
		}

		PageModel<PositionEntity> pageModel = positionService.queryByKeyWord(
				pageNo, Constants.PAGE_SIZE, keyWord);
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().print(JSON.toJSONString(pageModel));
	}

	/**
	 * 到position列表时查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String rowStr = request.getParameter("row");
		// 非登录操作
		if (rowStr != null) {
			int row = Integer.parseInt(rowStr);
			if (row == 1) {
				request.setAttribute("success", "操作成功！");
			} else {
				request.setAttribute("error", "操作失败！");
			}
		}
		int id = AppUtil.getCompanyId(request, response);
		// 职位列表
		List<PositionEntity> positionList = positionService.list(id);
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("/WEB-INF/view/company/position.jsp")
				.forward(request, response);
	}

	/**
	 * 跳转到新增职位界面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void toAddUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/company/position-add.jsp")
				.forward(request, response);
	}

	/**
	 * 删除职位
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		int row = positionService.delete(id);

		if (row == 0) {
			response.getWriter().print("fail");
		}
	}

	/**
	 * 发布招聘职位
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int companyId = AppUtil.getCompanyId(request, response);
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		String salary1 = request.getParameter("salary1");
		String salary2 = request.getParameter("salary2");
		List<Object> list = new ArrayList<>();
		list.add(companyId);
		list.add(name);
		list.add(duty);
		list.add(ability);
		list.add(salary1);
		list.add(salary2);
		int row = positionService.add(list);
		response.sendRedirect("PositionServlet?command=main&row=" + row);
	}

	/**
	 * 职位详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void detail(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (AppUtil.isLogin(request)) {
			// 职位详情
			int id = Integer.parseInt(request.getParameter("id"));
			PositionEntity position = positionService.detail(id);
			request.setAttribute("position", position);
			// 某求职者所有简历列表
			int talentId = AppUtil.getTalentId(request, response);
			List<ResumeEntity> resumeList = resumeService.list(talentId);
			request.setAttribute("resumeList", resumeList);
			// 验证此职位是否已经投递
			ApplicationEntity application = applicationService.isApply(
					talentId, id);
			request.setAttribute("application", application);

			request.getRequestDispatcher(
					"/WEB-INF/view/company/position-detail.jsp").forward(
					request, response);
		} else {
			request.setAttribute("msg", "请先登录，再执行简历投递！");
			request.getRequestDispatcher("/WEB-INF/view/talent/login.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 所有职位列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int pageNo = 1;
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null && !"".equals(pageNoStr)) {
			pageNo = Integer.parseInt(pageNoStr);
		}

		PageModel<PositionEntity> pageModel = positionService.queryAll(pageNo,
				Constants.PAGE_SIZE);
		request.setAttribute("pageModel", pageModel);
		request.getRequestDispatcher("/WEB-INF/view/company/position-list.jsp")
				.forward(request, response);
	}

	/**
	 * 职位修改（查询之前信息，显示到修改页面，方便后期参照修改）
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void preupdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2、调用业务逻辑
		PositionEntity position = positionService.queryById(id);
		// 3、转向
		if (position == null) {
			response.sendRedirect("CompanyServlet?command=main");
		} else {
			request.setAttribute("position", position);
			request.getRequestDispatcher(
					"/WEB-INF/view/company/position-update.jsp").forward(
					request, response);
		}
	}

	/**
	 * 修改职位信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int companyId = AppUtil.getCompanyId(request, response);
		int positionId = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		String salary1 = request.getParameter("salary1");
		String salary2 = request.getParameter("salary2");
		List<Object> list = new ArrayList<>();
		list.add(companyId);
		list.add(name);
		list.add(duty);
		list.add(ability);
		list.add(salary1);
		list.add(salary2);
		list.add(positionId);
		int row = positionService.update(list);
		response.sendRedirect("PositionServlet?command=main&row=" + row);
	}
}
